﻿function WindowPopup()
{
    this.MessageResult = null;
    this._kendoWindow = null;
    this.id;
    this.name;
    this.ControlAssign = null;

    this.onactivate = function (e)
    {
        $(".k-i-" + e.sender.element[0].id).click(function (e) {
            var id = $(e.target).attr('class').replace('ico-lang k-i-', '');
            
            for (i = 0; i < managewindow.length; i++) {
                if (managewindow[i].id == id) {
                    managewindow[i]._kendoWindow.wrapper.find('.k-content-frame')[0].contentWindow.ShowLanguage();
                    break;
                }
            }

            e.preventDefault();
        });
    }

    this.ShowMessageCode = function (title, MessageCode, Message, Button, callback, para1)
    {
        this.MessageResult = callback;
        this._kendoWindow = $("<div id='" + this.id + "'></div>").kendoWindow({
            modal: true,
            visible: false,
            scrollable: false,
            width: 400,
            height: 130,
            title :'AccNet i',
            iframe: true,
            resizable: false,
            actions: ["Close"],
            content: GetBaseUrl() +"Shared/MessageBox?messageCode=" + MessageCode + "&message=" + Message + "&Button=" + Button + "&id=" + this.id + "&Para1=" + para1,
            deactivate: function () {
                this.destroy();
            },
            close: function (e) { OnSafeWindow(e); },
            refresh: function () { this.center(); },
            open: function (e) {
                
                window.parent.HideToolbarWindowOfMetro(true);
            }

        }).data("kendoWindow");

        this._kendoWindow.center().open();
    };

    this.ShowMessage = function(title, Message, Button, callback) 
    {
        this.ShowMessageCode(title, "", Message, Button, callback);
    };

        
    this.CloseMessage = function (result)
    {
        if (this._kendoWindow)
        {
            this._kendoWindow.close();
            if (this.MessageResult)
            {
                this.MessageResult(result);
                this.MessageResult = null;
            }
            this._kendoWindow = null;
        }
    };


    this.OpenWindow = function (url, title, w, h, callback, _modal) {
        if (url.indexOf('=') >= 0)
            url = url + "&id=" + this.id;
        else
            url = url + "/?id=" + this.id;

        this.MessageResult = callback;
        if (_modal == false) {
            this._kendoWindow = $("<div  id='" + this.id + "'/>").kendoWindow({
                modal: false,
                visible: false,
                scrollable: false,
                width: w,
                height: h,
                title: title,
                iframe: true,
                content: url,
                activate: this.onactivate,
                actions: ["Minimize", "Close"],
                resizable :false,
                deactivate: function () {
                    this.destroy();
                },
                close:  function (e) { OnSafeWindow(e); },
                refresh: function () { this.center(); },
                open: function (e) {
                    $("#" + e.sender.element[0].id + "_wnd_title").prepend("<a style='display:inline-block;margin-top:-6px' href='#' role='button'><div style='top:5px;position:relative;margin-right:2px' class='ico-lang k-i-" + e.sender.element[0].id + "'></div></a>");
                    
                }

            }).data("kendoWindow");
        }
        else {
            this._kendoWindow = $("<div  id='" + this.id + "'/>").kendoWindow({
                modal: true,
                visible: false,
                scrollable: false,
                width: w,
                height: h,
                title: title,
                iframe: true,
                content: url,
                activate: this.onactivate,
                actions: [ "Close"],
                resizable: false,
                close: function (e) { OnSafeWindow(e); },
                deactivate: function () {
                    this.destroy();
                },
                refresh: function () { this.center(); },
                open: function (e) {
                    $("#" + e.sender.element[0].id + "_wnd_title").prepend("<a style='display:inline-block;margin-top:-6px' href='#' role='button'><div style='top:5px;position:relative;margin-right:2px' class='ico-lang k-i-" + e.sender.element[0].id + "'></div></a>");
                    window.parent.HideToolbarWindowOfMetro(true);
                }

            }).data("kendoWindow");
        }
        this._kendoWindow.center().open();
    };

    this.OpenWindowAuto = function (url, title, callback,_modal) {
        this.MessageResult = callback;
        var w = 0;
        var h = 0;

        if (typeof window.innerWidth == "undefined")
            w = $(window).width() * 0.9;
        else
            w = window.innerWidth * 0.9;

        if (typeof window.innerHeight == "undefined")
            h = $(window).height() * 0.9;
        else
            h = window.innerHeight * 0.9;

        this.OpenWindow(url, title, w, h, callback, _modal);
    };

    this.close = function ()
    {
        this._kendoWindow.close();
    };

    this.refresh = function ()
    {
        this._kendoWindow.refresh();
    };

    this.destroy = function ()
    {
        this._kendoWindow.destroy();
    };

    this.setLevel = function (l)
    {
        this.level = l;
    }
    this.restore = function ()
    {
        this._kendoWindow.restore();
    }
    this.toFront = function ()
    {
        this._kendoWindow.toFront();
    }

    function OnSafeWindow(e)
    {
        for (i = 0; i < managewindow.length; i++) {
            if (managewindow[i].id == e.sender.element[0].id) {
                if (typeof window.parent != "undefined" && typeof window.parent.ui != "undefined")
                {
                    window.parent.ui.closeApp(managewindow[i].name, true);
                }

                RemoveItemArray(managewindow, i);
            }
        }

        window.parent.HideToolbarWindowOfMetro(false);
    }

}